/*jslint sloppy: true */
/*global probe, angular */
probe.controller('CaseEditorSimple', function ($scope, ocDialog, ocAPI, $filter, GET_FRAME) {
	var DI = $scope.$parent.DI,
		pScope = $scope.$parent, //TODO controller as
		simpleCaseToCode = $filter('toSourceCode'),
		caseToSimpleCase = $filter("decodeSave"),
		maxLoopLimit = 200;

	$scope.instructionListRefresh = true;
	
	function refreshInstructionList() {
		$scope.instructionListRefresh = !$scope.instructionListRefresh;
	}
	$scope.shareFn = function () {
		return {
			ins: $scope.simpleCase.instructions,
			index: $scope.ILC.selected,
			dictionary: pScope.dictionary,
			edit: false,
			refresh: refreshInstructionList
		};
	};
	function modifyInstruction(instruction) {
		var instructionType = instruction.TYPE,
			names = {
				'0': 'addIns/wait',
				'1': 'addIns/action',
				'2': 'addIns/assert'
			};

		ocDialog.getNewDI(names[instructionType], angular.extend($scope.shareFn(), {
			edit: true
		})).then(function (newDI) {
			newDI.openDialogFrom(DI);
		});
	}
	function exchange(key1, key2) {
		var t = this[key1];
		this[key1] = this[key2];
		this[key2] = t;
	}

	$scope.ILC = $scope.instructionListControl = {
		header: [
			{
				key: 'ins',
				text: '指令',
				width: 80
			},
			{
				key: 'abst',
				text: '指令描述',
				width: 280
			}
		],
		selected: -1,
		visitFn: function (index) {
			modifyInstruction($scope.simpleCase.instructions[index]);
		}
	};
	$scope.deleteIns = function () {
		$scope.simpleCase.instructions.splice($scope.ILC.selected, 1);
	};
	$scope.up = function () {
		var sci = $scope.simpleCase.instructions,
			i = ($scope.ILC.selected -= 1);
		exchange.call(sci, i + 1, i);
		refreshInstructionList();
	};
	$scope.down = function () {
		var sci = $scope.simpleCase.instructions,
			i = ($scope.ILC.selected += 1);
		exchange.call(sci, i - 1, i);
		refreshInstructionList();
	};
	$scope.trashCase = {
		title: '警告 - 全部清空',
		message: '您确定要清空所有指令吗？',
		callFn: function () {
			$scope.simpleCase.instructions.length = 0;
		}
	};
	$scope.fillOrigin = function () {
		$scope.simpleCase.origin =
			GET_FRAME().contentWindow.location.href;
	};

	//init
	function initialize($case) {
		$scope.simpleCase = caseToSimpleCase($case.code).out;
		$scope.instructionListControl.selected = -1;
	}
	function convertToSource() {
		var $case = pScope['case'];
		if (!$scope.simpleCase) {
			return;
		}
		$case.code = simpleCaseToCode($scope.simpleCase, true);
		(pScope.refreshCodeModeCode || angular.noop)(simpleCaseToCode($scope.simpleCase, false));
	}
	$scope.$watchCollection('simpleCase', function () {
		convertToSource();
	});
	$scope.$watchCollection('simpleCase.instructions', function () {
		convertToSource();
	});
	$scope.$watch('$parent.case', function (newCase) {
		initialize(newCase);
	});
	
	pScope.simpleEmphasize = function (currentIns) {
		var line = currentIns.LINE;
		if (!line) {
			$scope.ILC.selected = -1;
			return;//main process does not have a line currently...
		}
		//offset
		if ($scope.simpleCase.noOrigin) {
			line -= 6;
		} else {
			line -= 7;
		}

		$scope.ILC.selected = line;
	};

	$scope.checkMaxLoop = function () {
		var maxLoop = parseInt($scope.simpleCase.maxLoop, 0);
		//do nothing if NaN
		if (maxLoop !== +maxLoop) {
			return;
		}
		maxLoop = Math.min(maxLoop, maxLoopLimit);

		$scope.simpleCase.maxLoop = maxLoop.toString();
	};
});
